package com.nus.servlets;

import com.nus.database.ProjectsDBManager;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

/**
 * Created by IntelliJ IDEA.
 * User: dell
 * Date: 9/27/12
 * Time: 12:16 AM
 * To change this template use File | Settings | File Templates.
 */
public class DeleteProjectServlet extends AbstractServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String projectIdParam = request.getParameter("projects");

        String errorMessage = null;

        if (projectIdParam == null) {
            errorMessage = "Please select a project.";
        } else {
            int projectId = Integer.parseInt(projectIdParam);
            Connection connection = getUserDatabaseConnection(request);
            ProjectsDBManager projectsDBManager = new ProjectsDBManager(connection);
            projectsDBManager.deleteProject(projectId);

            projectsDBManager.close();
        }

        request.setAttribute("ERROR_MESSAGE", errorMessage);

        RequestDispatcher requestDispatcher = request.getRequestDispatcher("viewProjects.do");
        requestDispatcher.forward(request, response);
    }
}
